<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head>
    <title> p2p网贷平台 </title>

    <link href="<%=basePath%>css/common.css" rel="stylesheet" />
    <link rel="stylesheet" type="text/css" href="<%=basePath%>css/user.css" />
    <link rel="stylesheet" type="text/css" href="<%=basePath%>css/jquery.datetimepicker.css"/>
    <script type="text/javascript" src="<%=basePath%>script/jquery.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>script/common.js"></script>
    <script src="<%=basePath%>script/user.js" type="text/javascript"></script>
    <link href="<%=basePath%>css/bootstrap.min.css" rel="stylesheet">
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="<%=basePath%>js/jquery.min.js"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="<%=basePath%>js/bootstrap.min.js"></script>
</head>
<body onload="checks();">
<jsp:include page="head.jsp"></jsp:include>


<div class="wrapper wbgcolor">
    <div class="w1200 personal">
        <!-- 导航栏 --><jsp:include page="left.jsp"></jsp:include>

        <div class="personal-main">
            <link rel="stylesheet" type="text/css" href="css/fileupload.less.css">
            <style type="text/css">
                .ui-fileupload-choose{
                    background:none;
                    width: 90px; height: 90px;
                    border:0px none;
                }
                .ui-fileupload-choose input{
                    width:100%;height:100%;
                }
                .ui-icon{
                    background:none;
                    width:0px;height:0px;
                }
            </style>
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">还款明细</h3>
                    </div>
                    <div class="panel-body">
                        <table class="table table-bordered">
                            <thead>
                            <tr>
                                <th>期数</th>
                                <th>月还本息</th>
                                <th >月还本金</th>
                                <th >月还利息</th>
                                <th >本息余额</th>
                                <th >还款截止时</th>
                                <th >还款状态</th>
                                <th >还款操作</th>
                            </tr>
                            </thead>
                            <tbody id="mytbdy">

                            </tbody>
                        </table>
                    </div>
                </div>
            <script type="text/javascript">
                function pay(ids,money,id,obj) {
                    //tables+="<a href='<%=basePath%>brower/tohuankuanupds.do?ids="+dept.boid+"&money="+yueHuanBenXi.toFixed(2)+"&id=${borr.id}'>还款</a>";
                    //alert("hello");
                    tmp = "";
                    tmp = tmp + "<%=basePath%>brower/tohuankuanupds.do?ids="+ids+"&money="+money+"&id="+id;
                    $.get(tmp,function (data,status) {
                        //alert("Data: " + data + "nStatus: " + status);
                        if (data == "0"){
                            alert("还款不成功，余额不足！");
                        }
                        else
                        {
                            console.log($(obj).parent());
                            $(obj).parent().prev().text("已还款");
                            $(obj).remove();

                        }
                    });
                }

                function checks(){

                    $.post('<%=basePath%>brower/tohuankuanupdison.do?id=${borr.id}',{},function(data){

                        var list= data;
                        var repayWay=${borr.bserial};//还款方式
                        var money=${borr.bmoney};//借款金额
                        var month=${borr.btimelimit};//期限
                        var annualRate=${borr.brate};//年化收益率


                        if(repayWay=='3'){
                            // 贷款本金×月利率×（1+月利率）还款月数/[（1+月利率）还款月数-1]
                            var annualRates=annualRate/100;
                            var monthrate= annualRates/12;
                            var num = monthrate; //月利率
                            var a=1+num;
                            var j=Math.pow(a,month);
                            var yueHuanBenXi=money*num*j/(j-1);
                            var ZongEr=yueHuanBenXi*month;
                            var Zong=yueHuanBenXi*month;
                            var tables="";

                            for(var i=1;i<=month;i++){

                                var benxiyuer=(i==1?ZongEr:ZongEr-yueHuanBenXi);
                                var monthlixi=benxiyuer*(num);//第一个月利息=本金*月利率  第二个月利息=（本息总额-月还本息）*月利率 以后类推
                                var monthBenjin=yueHuanBenXi-monthlixi;//月还本金=月还本息-月还利息
                                var x=benxiyuer;
                                var y=yueHuanBenXi;
                                var nums=x-y;
                                var dept=list[i-1];
                                ZongEr=benxiyuer;
                                tables=tables+"<tr><td>"+(i)
                                    +"</td><td >"+yueHuanBenXi.toFixed(2)
                                    +"</td><td >"+monthBenjin.toFixed(2)
                                    +"</td><td >"+monthlixi.toFixed(2)
                                    +"</td><td >"+nums.toFixed(2)
                                    +"</td><td >"+new Date(dept.bdate).toLocaleString()
                                    +"</td><td >";
                                if(dept.bstatue==1){
                                    tables+="已还款";
                                }else if (dept.bstatue==0) {
                                    tables+="未还款";
                                }
                                tables+="</td><td >";
                                if(dept.bstatue==1){

                                }else if (dept.bstatue==0) {
                                    //tables+="<a href='<%=basePath%>tohuankuanupds.do?ids="+dept.boid+"&money="+yueHuanBenXi.toFixed(2)+"&id=${borr.id}'>还款</a>";
                                    tables+="<button class='hello' onclick='pay("+dept.boid+","+yueHuanBenXi.toFixed(2)+",${borr.id},this)'>还款</button>";
                                }
                                tables+="</td></tr>";
                            }
                            $("#mytbdy").html(tables);
                        }
                        //一次还本付息=本金+每月利息*期数   这里的每月利息固定
                        if(repayWay=='1'){
                            var annualRates=annualRate/100;
                            var monthrate=annualRates/12;
                            var num = (monthrate.toFixed(5)); //月利率
                            var g=(num*money)*month;
                            var zong=parseFloat(money)+parseFloat(g);
                            var tables="<tr>";
                            var dept=list[0];
                            tables+="<td >"+month
                                +"</td><td >"+zong.toFixed(2)
                                +"</td><td >"+money
                                +"</td><td >"+g.toFixed(2)
                                +"</td><td >"+0.00
                                +"</td><td >"+new Date(dept.bdate).toLocaleString()
                                +"</td><td >";
                            if(dept.bstatue==1){
                                tables+="已还款";
                            }else if (dept.bstatue==0) {
                                tables+="未还款";
                            }
                            tables+="</td><td >";
                            if(dept.bstatue==1){

                            }else if (dept.bstatue==0) {
                                //tables+="<a href='<%=basePath%>tohuankuanupds.do?ids="+dept.boid+"&money="+yueHuanBenXi.toFixed(2)+"&id=${borr.id}'>还款</a>";
                                tables+="<button class='hello' onclick='pay("+dept.boid+","+yueHuanBenXi.toFixed(2)+",${borr.id},this)'>还款</button>";
                            }
                            tables+="</td></tr>";
                            $("#mytbdy").html(tables);
                        }
                        //先息后本 月还利息不变
                        if(repayWay=='2'){
                            var annualRates=annualRate/100;
                            var monthrate=annualRates/12;
                            var num =(monthrate.toFixed(5)); //月利率
                            var g=(num*money)*month;//总共要还的利息
                            var gs=num*money;
                            var zong=parseFloat(money)+parseFloat(g);
                            var zon=parseFloat(money)+parseFloat(g);
                            var tables="<tr>"

                            for(var i=1;i<=month;i++){
                                var dept=list[i-1];
                                var benxiyuer=(i==month?0.00:zong-gs);
                                zong=benxiyuer;
                                var yuehuanbenxi=(i==month?(parseFloat(money)+parseFloat(gs)):gs);
                                var yuehuanbenjin=i==month?money:0;
                                tables+="<td>"+(i)
                                    +"</td><td >"+yuehuanbenxi.toFixed(2)
                                    +"</td><td>"+yuehuanbenjin
                                    +"</td><td>"+gs.toFixed(2)
                                    +"</td><td>"+benxiyuer.toFixed(2)
                                    +"</td><td >"+new Date(dept.bdate).toLocaleString()
                                    +"</td><td >";
                                    if(dept.bstatue==1){
                                        tables+="已还款";
                                    }else if (dept.bstatue==0) {
                                        tables+="未还款";
                                    }
                                    tables+="</td><td >";
                                    if(dept.bstatue==1){

                                    }else if (dept.bstatue==0) {
                                        //tables+="<a href='<%=basePath%>tohuankuanupds.do?ids="+dept.boid+"&money="+yueHuanBenXi.toFixed(2)+"&id=${borr.id}'>还款</a>";
                                        tables+="<button class='hello' onclick='pay("+dept.boid+","+yueHuanBenXi.toFixed(2)+",${borr.id},this)'>还款</button>";
                                    }
                                tables+="</td></tr>";
                            }
                            $("#mytbdy").html(tables);

                        }

                    },"json");

                }
            </script>
            <div class="clear"></div>
        </div>
    </div>
    <!--网站底部-->
    <!-- 导航栏 -->
    <jsp:include page="bottom.jsp"></jsp:include>
</div>

</body>
</html>
